package com.example.springboot_shopsystem01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_shopsystem01.dao.GoodDao;
import com.example.springboot_shopsystem01.dao.StaffDao;
import com.example.springboot_shopsystem01.domain.Good;
import com.example.springboot_shopsystem01.domain.Staff;
import com.example.springboot_shopsystem01.service.GoodService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodDao goodDao;

    @Override
    public Boolean save(Good good) {
        return goodDao.insert(good) > 0;  //返回的数大于零就成功
    }

    @Override
    public Boolean update(Good good) {
        return goodDao.updateById(good) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return goodDao.deleteById(id) > 0;
    }

    @Override
    public Good getById(Integer id) {
        return goodDao.selectById(id);
    }

    @Override
    public List<Good> getAll() {
        return goodDao.selectList(null);
    }

    @Override
    public IPage<Good> getPage(int currentPage, int pageSize) {  //旧
        IPage page = new Page(currentPage, pageSize);
        goodDao.selectPage(page, null);
        return page;
    }

    @Override
    public IPage<Good> getPage(int currentPage, int pageSize, Good good) {  //可实现条件查询
        //增加条件查询
        LambdaQueryWrapper<Good> lqw = new LambdaQueryWrapper<Good>();  //构造条件查询
        lqw.like(Strings.isNotEmpty(good.getName()), Good::getName, good.getName());  //根据商品名称like查询
        lqw.like(good.getId() != null, Good::getId, good.getId());  //根据商品ID进行like查询

        //添加到page中
        IPage page = new Page(currentPage, pageSize);
        goodDao.selectPage(page, lqw);
        return page;
    }

}
